package com.hua.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hua.common.Response;
import com.hua.common.ResponseCode;
import com.hua.domain.Discountcard;
import com.hua.domain.Userbindingdiscountcard;
import com.hua.exception.BusinessException;
import com.hua.mapper.DiscountcardMapper;
import com.hua.mapper.UserbindingdiscountcardMapper;
import com.hua.service.DiscountcardService;
import com.hua.util.RedisCache;
import com.hua.util.ResultUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Distance
* @description 针对表【discountcard(优惠卷)】的数据库操作Service实现
* @createDate 2022-10-22 15:51:26
*/
@Service
public class DiscountcardServiceImpl extends ServiceImpl<DiscountcardMapper, Discountcard>
    implements DiscountcardService {

    @Resource
    private DiscountcardMapper discountcardMapper;

    @Resource
    private UserbindingdiscountcardMapper userbindingdiscountcardMapper;

    @Resource
    private RedisCache redisCache;
    @Override
    public Response<List<Discountcard>> getAllDiscounts(Long userId) {

        List<Discountcard> discountcards =
                redisCache.getCacheList("discountcards");

        if(discountcards.size()!=0){
            return ResultUtils.success(ResponseCode.SUCCESS,"查询成功");
        }

        LambdaQueryWrapper<Userbindingdiscountcard> queryWrapper
                = new LambdaQueryWrapper<>();
        queryWrapper.eq(Userbindingdiscountcard::getUid,userId);
        List<Userbindingdiscountcard> discountCard
                = userbindingdiscountcardMapper.selectList(queryWrapper);
        List<Integer> dId =
                discountCard.parallelStream().map(Userbindingdiscountcard::getDiscountId).collect(Collectors.toList());
         discountcards = discountcardMapper.selectBatchIds(dId);
        if (discountcards == null || discountcards.size() == 0) {
            throw new BusinessException(ResponseCode.SYSTEM_ERROR,"数据为空");
        }

        redisCache.setCacheList("discountcards",discountcards);

        return ResultUtils.success(ResponseCode.SUCCESS,discountcards,"","查询成功");
    }

    @Override
    public Response<String> sendDiscount(Long discountId) {
        Discountcard discountcard = discountcardMapper.selectById(discountId);
        discountcard.setStatus(1);
        if(discountcardMapper.updateById(discountcard)>0){
            return ResultUtils.success("操作成功");
        }
        throw new BusinessException(ResponseCode.SYSTEM_ERROR,"操作失败");
    }

    @Override
    public Response<String> createDiscount(Discountcard discountcard) {

        if(discountcardMapper.insert(discountcard)>0){
            return ResultUtils.success(ResponseCode.SUCCESS,"添加成功");
        }
        throw new BusinessException(ResponseCode.SYSTEM_ERROR,"添加失败");
    }

    @Override
    public Response<String> deleteDiscount(Integer discountcard) {

        LambdaQueryWrapper<Userbindingdiscountcard> queryWrapper
                = new LambdaQueryWrapper<>();
        queryWrapper.eq(Userbindingdiscountcard::getDiscountId,discountcard);
        if( discountcardMapper.deleteById(discountcard) > 0 &&userbindingdiscountcardMapper.delete(queryWrapper)>0 ){
            return ResultUtils.success(ResponseCode.SUCCESS,"删除成功");
        }
        throw new BusinessException(ResponseCode.SYSTEM_ERROR,"删除失败");
    }

    public Response<String> updateDiscount(Discountcard discountcard) {
        if(discountcardMapper.updateById(discountcard)>0){
            return ResultUtils.success(ResponseCode.SUCCESS,"更新成功");
        }
        throw new BusinessException(ResponseCode.SYSTEM_ERROR,"更新失败");
    }

    @Override
    public Response<String> updateDiscountStatus(Long discountId, Integer status) {
        Discountcard discountcard = discountcardMapper.selectById(discountId);
        discountcard.setStatus(status);
        if (discountcardMapper.updateById(discountcard)>0){
            return ResultUtils.success(ResponseCode.SUCCESS,"更新成功");
        }
        throw new BusinessException(ResponseCode.SYSTEM_ERROR,"更新失败");
    }
}




